//index.js
import api from '../../../utils/api.js'
//获取应用实例
var app = getApp();

Page({
  data: {
    newpage: 1,
    recommendpage: 1,
    recommendlength: 1,
    newlength: 1,
    classifyId: '',
    name: '',
    imageurl: api.imageurl,
    byrecommend: 1,//按推荐查询
    bynew: 0,//按最新查询
    newcookbooks: [], //全部最新菜品
    recommendcookbooks: [], //推荐菜品
    cooking: [],
    selectCooking: "",// 当前选中烹饪方式
    cookbooks: [],
    infos: []
  },
  onLoad: function (option) {
    var that = this;
    api.getIndexData({
      success: function (res) {
        // console.log(res)
        var data = res.data.result;
        // console.log("data", data)
        var cookbooks = data.jingcookbooks;
        that.setData({
          cookbooks: cookbooks,
        })
      }
    })
    that.getInfos()
  },

  //获取所有菜品
  getCooking: function () {
    var that = this

    api.getCooking({
      success: function (res) {
        var cooking = res.data.result;
        that.setData({
          cooking: cooking,
        })
      }
    })
  },
  //根据烹饪方式获取菜谱
  getcookingCookList: function (e) {
    var that = this;
    var selectCooking = e.currentTarget.dataset.cookId;
    that.setData({
      selectCooking: selectCooking,
      newpage: 1,
      recommendpage: 1,
      // recommendlength:0,
      // newlength:0,
      newcookbooks: [],
      recommendcookbooks: [],
    })
    if (that.data.byrecommend == 1) {
      that.getRecommendCookList();
    } else {
      that.getNewCookList();
    }
  },
  //获取推荐菜谱
  getRecommendCookList: function () {
    var that = this
    that.setData({
      byrecommend: 1,
      bynew: 0,
      // newlength: 0,
      newpage: 1,
      newcookbooks: [],
    })
    api.getSonCookbookList({
      data: {
        page: that.data.recommendpage,
        id: that.data.classifyId,
        recommend: 1,
        cooking: that.data.selectCooking,
      },
      success: function (res) {
        var cookbooks = res.data.result;
        var cookbookss = that.data.recommendcookbooks
        for (var i in cookbooks) {
          cookbookss.push(cookbooks[i])
        }
        if (cookbookss.length <= 0) {
          that.setData({
            recommendlength: 0,
          })
        }
        that.setData({
          recommendlength: cookbookss.length,
          recommendcookbooks: cookbookss,
          recommendpage: that.data.recommendpage + 1
        })
      }
    })
  },
  //获取最新菜谱
  getNewCookList: function () {
    var that = this
    that.setData({
      byrecommend: 0,
      bynew: 1,
      // recommendlength: 0,
      recommendpage: 1,
      recommendcookbooks: [],
    })
    api.getSonCookbookList({
      data: {
        page: that.data.newpage,
        id: that.data.classifyId,
        isnew: 1,
        cooking: that.data.selectCooking,
      },
      success: function (res) {
        var cookbooks = res.data.result;
        var cookbookss = that.data.newcookbooks
        for (var i in cookbooks) {
          cookbookss.push(cookbooks[i])
        }
        if (cookbookss.length <= 0) {
          that.setData({
            newlength: 0,
          })
        }
        that.setData({
          newlength: cookbookss.length,
          newcookbooks: cookbookss,
          newpage: that.data.newpage + 1
        })
      }
    })
  },
  // 查看详情
  showDetail: function (e) {
    var cookId = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var praiseCount = e.currentTarget.dataset.praise;
    wx.navigateTo({
      url: '../detail/detail?cookId=' + cookId + "&name=" + name + "&praise=" + praiseCount,
    })
  },

  onReachBottom: function () {
    var that = this;
    if (that.data.byrecommend == 1) {
      that.getRecommendCookList()
    }
    if (that.data.bynew == 1) {
      that.getNewCookList()
    }
  },

  toNavigate: function () {
    wx.navigateTo({
      url: '../detail/detail',
    })
  },

  /**
     * 分享
     */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: "小巴依",
      path: '/pages/cookbook/list/list?classifyId=' + that.data.classifyId + '&name=' + that.data.name
    }
  },
  // 获取信息
  getInfos: function () {
    var that = this;
    // var page = that.data.page;
    api.getInfos({
      success: function (res) {
        // console.log(res)
        var infos = res.data.result;
        that.setData({
          infos: infos,
        })
      }
    })
  },
  /**
   * 联系痛快
   */
  callTongkuai: function () {
    var that = this;
    var phoneNum = that.data.infos[0].phone;
    wx.makePhoneCall({
      phoneNumber: phoneNum
    })
  },
})
